package controllers.features;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JLabel;

import models.games.ColoriEParole;
import models.games.ConfrontoRapido;
import models.games.Game;
import models.games.GocceDiParole;
import models.games.LuceRiflessa;
import models.games.MemoriaOrdinata;
import models.games.MemorizzaTabella;
import models.games.PallonciniMatematici;
import models.games.PesciDisorientati;
import models.games.SchiacciaArancia;
import models.games.SchiacciaTalpe;
import views.features.GameMenuView;
import views.games.ColoriEParoleView;
import views.games.ConfrontoRapidoView;
import views.games.GameView;
import views.games.GocceDiParoleView;
import views.games.LuceRiflessaView;
import views.games.MemoriaOrdinataView;
import views.games.MemorizzaTabellaView;
import views.games.PallonciniMatematiciView;
import views.games.PesciDisorientatiView;
import views.games.SchiacciaAranciaView;
import views.games.SvegliaTalpeView;
import controllers.games.ColoriEParoleController;
import controllers.games.ConfrontoRapidoController;
import controllers.games.GocceDiParoleController;
import controllers.games.LuceRiflessaController;
import controllers.games.MemoriaOrdinataController;
import controllers.games.MemorizzaTabellaController;
import controllers.games.PallonciniMatematiciController;
import controllers.games.PesciDisorientatiController;
import controllers.games.SchiacciaAranciaController;
import controllers.games.SvegliaTalpeController;
import core.GameLauncher;
import core.GameLauncher.State;
import core.constants.Constants;

public class GameMenuController extends MouseAdapter {

	private GameMenuView view;
	private GameLauncher gameLauncher;

	private Game model;
	private GameView gameView;
	private ActionListener controller;
	private static GameMenuController instance;

	public static GameMenuController newInstance(GameMenuView view,
			GameLauncher gameFrame) {
		if (instance == null)
			instance = new GameMenuController(view, gameFrame);
		return instance;
	}

	private GameMenuController(GameMenuView view, GameLauncher gameFrame) {
		this.view = view;
		this.gameLauncher = gameFrame;
		view.addMouseListenerToGames(this);

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		JLabel source = (JLabel) e.getSource();
		String gameClicked = source.getName();
		System.out.println("Cliccato: " + gameClicked);

		switch (gameClicked) {
		case "Confronto Rapido":
			model = new ConfrontoRapido(gameLauncher.getUser());
			gameView = new ConfrontoRapidoView();
			controller = new ConfrontoRapidoController((ConfrontoRapido) model,
					(ConfrontoRapidoView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Schiaccia Arancia":
			model = new SchiacciaArancia(gameLauncher.getUser()); // lo creo con
																	// null ma
																	// l'utente
																	// viene
																	// impostato
																	// dalla
																	// classe
																	// GameLauncher
																	// invocando
																	// il metodo
																	// setUser
																	// di Game
			gameView = new SchiacciaAranciaView(2, 2);
			controller = new SchiacciaAranciaController(
					(SchiacciaArancia) model, (SchiacciaAranciaView) gameView,
					gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Memorizza Tabella":
			model = new MemorizzaTabella(gameLauncher.getUser());
			gameView = new MemorizzaTabellaView(2);
			controller = new MemorizzaTabellaController(
					(MemorizzaTabella) model, (MemorizzaTabellaView) gameView,
					gameLauncher);
			gameLauncher.setGame(model, gameView, controller);
			gameLauncher.startTransition(State.GAME);
			break;
		case "Luce Riflessa":
			model = new LuceRiflessa(gameLauncher.getUser());
			gameView = new LuceRiflessaView();
			controller = new LuceRiflessaController((LuceRiflessa) model,
					(LuceRiflessaView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Colori e Parole":
			model = new ColoriEParole(gameLauncher.getUser());
			gameView = new ColoriEParoleView(
					((ColoriEParole) model).casualStringColor(),
					((ColoriEParole) model).getColorMeaning(),
					((ColoriEParole) model).getColoredWord());
			controller = new ColoriEParoleController((ColoriEParole) model,
					(ColoriEParoleView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Gocce di Parole":
			try {
				model = new GocceDiParole(gameLauncher.getUser());
			} catch (SQLException | IOException e1) {
				e1.printStackTrace();
			}
			gameView = new GocceDiParoleView(
					((GocceDiParole) model).getFirstString());
			controller = new GocceDiParoleController((GocceDiParole) model,
					(GocceDiParoleView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Memoria Ordinata":
			model = new MemoriaOrdinata(gameLauncher.getUser());
			gameView = new MemoriaOrdinataView(
					((MemoriaOrdinata) model).getShuffledNumbers());
			controller = new MemoriaOrdinataController((MemoriaOrdinata) model,
					(MemoriaOrdinataView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Palloni Matematici":
			model = new PallonciniMatematici(gameLauncher.getUser());
			gameView = new PallonciniMatematiciView();
			controller = new PallonciniMatematiciController(
					(PallonciniMatematici) model,
					(PallonciniMatematiciView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;

		case "Pesci Disorientati":
			model = new PesciDisorientati(gameLauncher.getUser());
			gameView = new PesciDisorientatiView();
			controller = new PesciDisorientatiController(
					(PesciDisorientati) model,
					(PesciDisorientatiView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		case "Sveglia la Talpa":
			model = new SchiacciaTalpe(gameLauncher.getUser());
			gameView = new SvegliaTalpeView(((SchiacciaTalpe) model).getTable());
			controller = new SvegliaTalpeController((SchiacciaTalpe) model,
					(SvegliaTalpeView) gameView, gameLauncher);
			// gameLauncher.setGame(model, gameView, controller);
			// gameLauncher.startTransition(State.GAME);
			break;
		}

		gameLauncher.setGame(model, gameView, controller);
		gameLauncher.startTransition(State.GAME);

	}

}
